<!-- 登录模块 -->

<template>
  <div class="login">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
    >
      <h3 class="title">若依后台管理系统</h3>

      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          placeholder="账号"
        ></el-input>

        <!-- 密码 -->
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="密码"
        ></el-input>

        <!-- 验证码 -->
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          style="width: 63%"
          placeholder="验证码"
        ></el-input>
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode" />
        </div>
      </el-form-item>

      <el-checkbox
        style="margin: 0px 0px 25px 0px"
        v-model="loginForm.rememberMe"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          @click.native="handelLogin"
          style="width: 100%"
        >
          <span>登 录</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImgAPI } from '../api/login'

export default {
  name: 'login',
  data () {
    return {
      // 表单
      loginForm: {
        username: 'admin', // 用户名
        password: 'admin123', // 密码
        code: '', // 验证码
        uuid: '', // 登录需要
        rememberMe: false // 选择框
      },
      codeUrl: '', // 验证码图片
      captchaEnabled: true,
      redirect: undefined, // 路由参数

      // 表单校验
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      }
    }
  },

  watch: {
    // 监听路由变化
    $route: {
      handler (route) {
        console.log('route路由变化', route)
        this.redirect = route.query && route.query.redirect
      },
      immediate: true // 开启立刻监听
    }
  },

  created () {
    this.getCode()
  },

  methods: {
    // 获取验证码图片
    async getCode () {
      try {
        // 调用接口
        const res = await getCodeImgAPI()
        this.loginForm.uuid = res.uuid
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img // 将图片数据直接嵌入到文档中的编码策略，可以将图片的二进制数据转换成 base64 字符串
        }
      } catch (error) {
        console.log('error  ----->  ', error)
      }
    },

    // 登录
    async handelLogin () {
      // 表单校验
      this.$refs.loginForm.validate(async valid => {
        // valid是校验的字段 , 校验不通过返回false , 限制接口频繁触发
        if (valid) {
          // dispatch触发Vuex中的Actions
          this.$store.dispatch('registerLogin', this.loginForm).then(res => {
            console.log('登录成功', res)
            // 跳转路由
            this.$router.push({ path: this.redirect || '/' })
          }).catch(error => {
            console.log('登录失败', error)
            // 重新获取验证码
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  display: flex;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  justify-content: center;
  align-items: center;
}
.title {
  color: #707070;
  text-align: center;
  margin: 0px auto 30px;
}
.login-form {
  box-sizing: border-box;
  width: 400px;
  border-radius: 6px;
  background: #fff;
  padding: 25px 25px 5px 25px;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0px;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
  font-family: Arial;
}
.login-code {
  width: 33%;
  float: right;
  img {
    cursor: pointer;
  }
}
.login-code-img {
  height: 38px;
}
</style>
